/**
 * 获取选中活动的数据，并且显示
 * @param id
 * @param title
 */
function getNextActivityAndShow(){

    showLineDetail()

}


/**
 * 当两个活动有双向连线时，切换source和target
 */
function toggleSourceAndTarget(){

    getNextActivityAndShow();
}

function onactiveFormula(){
    mini.open({
        url: "base/dialog/selectCondition.html",
        title: "配置条件", width: 1000, height: 600,
        onload: function () {
            //给对话框传参数！
            //var iframe = this.getIFrameEl();
            //iframe.contentWindow.SetData("");
        },
        ondestroy: function (e) {
            if (e == "OK") {
            }
        }
    });

}

/*
* @author: wj
* @description: 创造连线
* @date: 2019/3/5 20:11
* @param
* @return
*/
function createConnection(sourceId,sourcePoint,targetId,targetPoint,wfId){
    var url = '/wf/activity/line/createNewActivityLine';
    ActHttpRequest(url,{
        'sourceId':sourceId,
        'sourcePoint':sourcePoint,
        'targetId':targetId,
        'targetPoint':targetPoint,
        'wfId':wfId
    },function(data){
        if(!data){
            layer.alert('程序发生错误，请联系管理员处理！');
            return;
        }
        if(data.OK !== "OK"){
            var connections = flowchart.instance.getConnections({
                source: sourceId,
                target: targetId
            });
            if(connections.length > 0)
                flowchart.instance.deleteConnection(connections[0]);
            layer.alert(data.message);
        }

    },'post',null,null,'读取中。。。',true);
}

/*
* @author: wj
* @description: 活动连线删除
* @date: 2019/3/6 13:40
* @param
* @return
*/
function deleteNextActivity(){

    var sourceId = flowchart.currentLine.sourceId;
    var targetId = flowchart.currentLine.targetId;

    layer.confirm("删除后不可恢复，是否继续？",{icon: 2, title:'删除确认'}, function(index){

        var url = '/wf/activity/line/delLineBySourceTargetWfid';
        ActHttpRequest(url,{
            'sourceId': sourceId,
            'targetId': targetId,
            'wfId': wfId
        },function(data){
            if(typeof data.OK === 'undefined'){
                layer.alert('程序发生错误，请联系管理员处理！');
                return;
            }
            if(data.OK != "OK"){
                layer.alert(data.message);
                return;
            }

            flowchart.instance.deleteConnection(flowchart.currentLine)
            layer.close(index)
            layer.close(currentLayerIndex)
            flowchart.currentLine = {}

        },null,null,null,'读取中。。。',true);
    });
}

function showHelpWinNextActivity(){
    mini.alert('还没想好！');
}

function getAllNextActivitiesAndConnect(){
    var url = '/wf/activity/line/list';
    ActHttpRequest(url,{
        'wfId':wfId
    },function(data){
        if(!data){
            layer.msg('程序发生错误，请联系管理员处理！');
            return;
        }
        if(data.OK !== "OK"){
            layer.msg(data.message);
            return;
        }
        var conns = data.List,
            sourceId,sourcePoint,targetId,targetPoint,conditions;
        for(var i = 0, j = conns.length;i < j; i++){
            sourceId = conns[i].sourceId;
            sourcePoint = conns[i].sourcePoint;
            targetId = conns[i].targetId;
            targetPoint = conns[i].targetPoint;
            conditions = conns[i].conditions;
            connectSourceToTarget(sourceId,sourcePoint,targetId,targetPoint,conditions)
        }

        //开始到001的连线变色
        var START_LINE_COLOR = "lightgray"
        var idStart = $("[activity_type='start']").attr("id")
        var id001 = $("[code='001']").attr("id")
        var line = flowchart.instance.getConnections({
            source: idStart,
            target: id001
        });
        if(line.length > 0){
            var idStartPoint = line[0].endpoints[0].anchor;
            var id001Point = line[0].endpoints[1].anchor;
            flowchart.instance.deleteConnection(line[0]);

            flowchart.instance.connect({
                source:idStart,
                target:id001,
                anchors:[[idStartPoint.x, idStartPoint.y], [id001Point.x, id001Point.y]],
                connector: ["Flowchart", {
                    stub: [36, 36],
                    gap: 3,
                    cornerRadius: 5,
                    alwaysRespectStubs: false
                }],
                connectionsDetachable: false,
                endpointStyle: {
                    radius: 0.1,
                    fill: START_LINE_COLOR
                },
                paintStyle: {
                    outlineStroke: START_LINE_COLOR,
                    strokeWidth: 0
                }
            });

            line = flowchart.instance.getConnections({
                source: idStart,
                target: id001
            });
            line[0].addOverlay(["Arrow", {
                location: 1,
                width: 12,
                length: 12,
                paintStyle: {
                    fill: START_LINE_COLOR
                }
            }]);
        }
        createConnectionFlag = true;
    },null,null,null,'读取中。。。',true);
}

/**
 * 连接两个point
 * 检查是否有反向连接，
 * 如果没有，直接添加connection
 * 如果有，则修改反向连接的Overlay，添加对应的Arrow；并且对照已有的connection修改连接的sourcePoint和targetPoint
 */
function connectSourceToTarget(sourceId,sourcePoint,targetId,targetPoint,conditions){
        var connLabel = ""
        if (conditions) connLabel = conObj2html(JSON.parse(conditions))

        flowchart.instance.connect({
            source:sourceId.toString(),
            target:targetId.toString(),
            anchors:[sourcePoint, targetPoint],
            overlays:[
                [ "Label", {
                    label:connLabel,
                    location:0.36,
                    id:"conLabel",
                    labelStyle:{
                        color:"white",
                        fill:"gray"
                    }
                }
                ]
            ]

        }, flowchart.commonStyle);
}
